package com.sjm.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.request.OapiUserGetbyunionidRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.dingtalk.api.response.OapiUserGetbyunionidResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.sjm.demo.service.TestUserService;
import com.sjm.demo.utils.ResultData;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjm
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/test-user")
public class TestUserController {

    @Autowired
    private TestUserService userService;

    @GetMapping("/getAppIdAndRedirectUri")
    public ResultData getAppIdAndRedirectUri(){
        ResultData resultData = new ResultData();
        JSONObject json = userService.getAppIdAndRedirectUri();
        return resultData.success(json);
    }

    @PostMapping(value = "/getUserInfo")
    public ResultData getUserInfo(@RequestParam("code") String code) {
        ResultData resultData = new ResultData();

        /*
        根据unionid获取userid，需要创建企业内部应用（小程序或微应用），使用内部应用的Appkey和AppSecret调用接口access_token。步骤一中创建的登录应用的appId和AppSecret，不支持获取access_token。
         */
        // String unionid = userService.getUnionidByCode(code);
        // if(unionid == null){
        //     return resultData.error(503,null,"获取unionid失败");
        // }
        // System.out.println("获取unionid");
        // String token = userService.getAccessTokenByAppIdAndSecret();
        // if(token == null){
        //     return resultData.error(503,null,"获取token失败");
        // }
        // System.out.println("获取token");
        // String userId = userService.getUserId(unionid,token);
        // if(userId == null){
        //     return resultData.error(503,null,"获取用户编号失败");
        // }
        // System.out.println("获取userId");
        // Object userInfo = userService.getUserInfoByUserId(userId,token);
        // if(userInfo == null){
        //     return resultData.error(503,null,"获取用户信息失败");
        // }
        System.out.println(code);
        String userInfo = userService.getUserInfoByCode(code);
        if(userInfo == null){
            return resultData.error(503,null,"获取userInfo失败");
        }
        System.out.println("获取userInfo");
        return resultData.success(userInfo,"用户详情");
    }

}
